﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.IO;
using System.Xml.Serialization;
using Common;

namespace Database
{
    /// <summary>
    /// 怪物数据库
    /// </summary>
    public class MonsterDatabase
    {
        private static byte[] Monster黑风兽Code = new byte[] { 0x71, 0x10, 0x00, 0x00 };
        private static byte[] Monster僵尸刺客Code = new byte[] { 0x7c, 0x10, 0x00, 0x00 };
        private static byte[] Monster怨灵鬼魂Code = new byte[] { 0x6A, 0x10, 0x00, 0x00 };
        private static byte[] Monster渔人头目Code = new byte[] { 0x89, 0x10, 0x00, 0x00 };

		private static Monster Monster风木鬼族LV73 = new Monster("风木鬼族", 73, new byte[] { 0xD4, 0x10, 0x00, 0x00 });
        private static Monster Monster岩岛毒蛇LV73 = new Monster("岩岛毒蛇", 73, new byte[] { 0xD4, 0x10, 0x00, 0x00 });
        private static Monster Monster裂化渔人LV75 = new Monster("裂化渔人", 75, new byte[] { 0xD7, 0x10, 0x00, 0x00 });
        private static Monster Monster邪毒水妖LV76 = new Monster("邪毒水妖", 76, new byte[] { 0xD8, 0x10, 0x00, 0x00 });
        private static Monster Monster血浸骷髅LV77 = new Monster("血浸骷髅", 77, new byte[] { 0xDB, 0x10, 0x00, 0x00 });
        private static Monster Monster绝境冰妖LV78 = new Monster("绝境冰妖", 78, new byte[] { 0xDF, 0x10, 0x00, 0x00 });
        private static Monster Monster绝地霸王龙LV79 = new Monster("绝地霸王龙", 79, new byte[] { 0x09, 0x11, 0x00, 0x00 });
        private static Monster Monster火焰霸王龙LV80 = new Monster("火焰霸王龙", 80, new byte[] { 0x79, 0x11, 0x00, 0x00 });
        private static Monster Monster魔犬LV81 = new Monster("魔犬", 81, new byte[] { 0x7A, 0x11, 0x00, 0x00 });
        
        
        private static Dictionary<string, Monster> monsterDict = new Dictionary<string, Monster>(256);
        private static List<Monster> monsterList = null;

        public static void Init()
        {
            Console.WriteLine("开始加载怪物数据库....");
            Load();

            if (monsterList == null)
            {
                monsterList = new List<Monster>(256);
                monsterList.Add(Monster岩岛毒蛇LV73);
                monsterList.Add(Monster裂化渔人LV75);
                monsterList.Add(Monster邪毒水妖LV76);
                monsterList.Add(Monster血浸骷髅LV77);
                monsterList.Add(Monster绝境冰妖LV78);
                monsterList.Add(Monster绝地霸王龙LV79);
                monsterList.Add(Monster火焰霸王龙LV80);
                monsterList.Add(Monster魔犬LV81);
				monsterList.Add(Monster风木鬼族LV73);

                Save();
            }

            foreach (Monster m in monsterList)
            {
				monsterDict.Add(m.Name,m);
                Console.WriteLine("怪物：" + m.Name + "\t LV" + m.Level + "\tCode:" + m.CodeString);
            }
            Console.WriteLine("加载怪物数据库完成。");
        }

        /// <summary>
        /// 根据等级获取怪物
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static List<Monster> GetMonster(int level)
        {
            List<Monster> list = new List<Monster>();
            foreach (Monster m in monsterList)
            {
                if (m.Level == level)
                {
                    list.Add(m);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据等级获取第一个等级相同的怪物
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static Monster GetFirstMonster(int level)
        {
            foreach (Monster m in monsterList)
            {
                if (m.Level == level)
                {
                    return m;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据怪物名称获取怪物
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Monster GetMonster(string name)
        {
            return monsterDict[name];
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void Save()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "//data//monster.xml";
            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Monster>));
                    serializer.Serialize(stream, monsterList);
                    stream.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("保存怪物数据库出错，错误信息：");
                Tools.PrintException(ex);
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public static void Load()
        {
            try
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\data\\monster.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Monster>));
                    monsterList = (List<Monster>)serializer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("加载怪物数据库出错，已采用程序内置数据，错误信息：");
                Tools.PrintException(ex);
            }
        }
    }
}
